﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Vit.Common;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Providers.Data;
using Vit.Web.Helpers;

namespace Vit.AdminModule.Areas.Admin.Providers
{
    public class SqlUserDataProvider : DbContextDataProvider<WebProvidersDb, User>
    {
        protected override IQueryable<User> OrderByDefault(IQueryable<User> query)
        {
            return query.OrderBy(o => o.Account);
        }

        public override object[] GetKeys(User obj)
        {
            return new object[] { obj.Account };
        }

        public override User GetByKeys(params object[] ids)
        {
            if (ids == null || ids.Length != 1)
                return null;
            string key = ids[0].ToString();
            return db.Users.FirstOrDefault(o => o.Account == key);
        }

        public override User SetDefaultValues(User obj)
        {
            if (obj.Site == null)
            {
                obj.Site = PortalContext.Current.Site;
            }
            return obj;
        }

        public override User New()
        {
            return PortalHelper.CreateAnonymousUser();
        }
    }
}
